package com.bjpowernode.crm.service.impl;

import com.bjpowernode.crm.exception.RepeatException;
import com.bjpowernode.crm.mapper.TypeMapper;
import com.bjpowernode.crm.mapper.ValueMapper;
import com.bjpowernode.crm.pojo.Type;
import com.bjpowernode.crm.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;

    @Autowired
    private ValueMapper valueMapper;


    public List getAll() {
        return typeMapper.getAll();
    }

    public Type get(String id) {
        return typeMapper.get(id);
    }

    @Override
    public void edit(Type type) {
        typeMapper.edit(type);
    }

    @Override
    public List getRel(String[] id) {
        return typeMapper.getRel(id);
    }

    @Override
    public void del(String[] id) {
        typeMapper.del(id);
    }

    public void delForce(String[] id) {
        // 先删除关联信息，防止约束导致删除失败
        valueMapper.delByTypeId(id);

        del(id);
    }

    public void save(Type type) {

        Type type2 = get(type.getId());

        if (type2 != null) {
            throw new RepeatException("编号重复！");
        }

        typeMapper.save(type);
    }
}
